package com.jlf.core.engine.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jlf.core.annotation.Domain;
import com.jlf.core.domain._SimpleDO;
import com.jlf.core.engine.constant.SchemaConstant;
import com.jlf.tool.toolkit.BitToolkit;
import lombok.Getter;
import lombok.Setter;

/**
 * 模型的查询条件配置
 *
 * @author wujr
 * 2023/7/21
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/21 1.0 新增]
 */
@Getter
@Setter
@Domain(code = "schemaCriterion", name = "领域检索条件", table = "e_jlf_schema_criterion")
public class SchemaCriterionDO extends _SimpleDO<SchemaCriterionDO> {
    /**
     * 所属的模型 {@link SchemaDomainDO#getDomainCode()}
     */
    private String domainCode;
    /**
     * 查询条件的编码
     */
    private String criterionCode;
    /**
     * 该查询字段对应的模型字段 {@link SchemaFieldDO#getClassFieldCode()}
     * 如果该查询条件需要支持多个字段，则多个字段用逗号分隔，字段之间用or关联
     */
    private String classFieldCode;
    /**
     * 对应的数据类型 {@link SchemaFieldDO#getClassDataType()}
     */
    private Integer classDataType;
    /**
     * 支持的检索方式 {@link com.jlf.core.engine.constant.SchemaConstant.Jdbc.Criterion}
     * 第1位：eq
     * 第2位：ne
     * 第3位：like (l)
     * 第4位：not like (nl)
     * 第5位：gt
     * 第6位：ge
     * 第7位：lt
     * 第8位：le
     * 根据该检索方式，生成的检索条件为：
     * {@code
     * class XxxCriteria{
     *     private Integer a;  // eq
     *     private N n = new N(); // ne
     *     class N{
     *         private Integer a; // ne
     *     }
     *     class Gt{
     *         private Integer a;
     *     }
     *     private Gt gt = new Gt();
     *     class Ge{
     *         private Integer a;
     *     }
     *     private Ge ge = new Ge();
     *     class Lt{
     *         private Integer a;
     *     }
     *     private Lt lt = new Lt();
     *     class Le{
     *         private Integer a;
     *     }
     *     private Le le = new Le();
     *     class L{
     *         private String key; // like
     *     }
     *     private L l = new L();
     *     class Nl{
     *         private String key; // not like
     *     }
     *     private Nl nl = new Nl();
     * }
     * }
     */
    private Integer supportCriterion;


    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isEqCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.eq - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isNeCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.ne - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLikeCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.like - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isNotLikeCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.notLike - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isNotLikeLeftCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.notLikeLeft - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isNotLikeRightCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.notLikeRight - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLikeLeftCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.likeLeft - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLikeRightCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.likeRight - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isGtCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.gt - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isGeCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.ge - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLtCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.lt - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLeCriterion(){
        return BitToolkit.isBit(this.supportCriterion, SchemaConstant.Jdbc.Criterion.le - 1);
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isCriterionSupported(int condition){
        return BitToolkit.isBit(this.supportCriterion, condition - 1);
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isCriterionValid(){
        return (this.supportCriterion & SchemaConstant.Jdbc.Criterion.mask) != 0;
    }
}
